package com.uton.rfidcontroller.view.activity.write;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.uton.rfidcontroller.R;
import com.uton.rfidcontroller.common.GlobalConstants;
import com.uton.rfidcontroller.common.intent.IntentBuilder;
import com.uton.rfidcontroller.databinding.ActivityWriteBinding;
import com.uton.rfidcontroller.model.CarListModel;
import com.uton.rfidcontroller.view.activity.CarDealerDetailActivity;
import com.uton.rfidcontroller.view.adapter.write.WriteRVAdapter;
import com.uton.rfidcontroller.view.base.BaseActivity;
import com.uton.rfidcontroller.viewmodel.activity_model.WriteActivityViewModel;

import java.util.List;

import butterknife.BindView;
import io.reactivex.ObservableTransformer;

/**
 * Created by 王家宝 on 2017/9/26.
 * Email:639586819@qq.com
 * Tel:18241150668
 */

public class WriteActivity extends BaseActivity<ActivityWriteBinding, WriteActivityViewModel> {

    @BindView(R.id.rv_write_ori)
    RecyclerView mRecyclerView;

    private WriteRVAdapter mAdapter;

    @Override
    protected int setLayout() {
        return R.layout.activity_write;
    }

    @Override
    protected void inject() {
        getBinding().setWriteModel(getViewModel());
    }

    @Override
    protected String setTitle() {
        return getString(R.string.ui_write);
    }

    @Override
    protected void doSomething() {

    }

    @Override
    protected WriteActivityViewModel setViewModel() {
        return new WriteActivityViewModel(getIntent().
                getStringExtra(GlobalConstants.EXTRA_STRING_PARAM1),
                getIntent().getStringExtra(GlobalConstants.EXTRA_STRING_PARAM2)) {

            @Override
            public void beginInit() {
                mAdapter = new WriteRVAdapter(WriteActivity.this);
                mRecyclerView.setAdapter(mAdapter);
                mRecyclerView.setLayoutManager(new LinearLayoutManager(WriteActivity.this));
            }

            @Override
            public void doOnComplete() {
                disDialog();
            }

            @Override
            public void doOnRequestComplete(List<CarListModel> data) {
                mAdapter.setDataList(data);
            }

            @Override
            public void showCarDealerDetail() {
                startActivity(new IntentBuilder().from(WriteActivity.this, CarDealerDetailActivity.class).
                        setStringParam(GlobalConstants.EXTRA_STRING_PARAM1, getIntent().
                                getStringExtra(GlobalConstants.EXTRA_STRING_PARAM1)).getIntent());
            }

            @Override
            public void showDialog() {
                showProgressDialog(null, false);
            }

            @Override
            public void dismissDialog() {
                disDialog();
            }

            @Override
            public ObservableTransformer setObservable() {
                return compose(bindToLifecycle());
            }
        };
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode) {
            case GlobalConstants.RESULT_COMMIT_SUCCESS:
                getViewModel().refreshData();
                break;
            default:
                break;
        }
    }
}
